package com.neu.admin;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.checkerframework.checker.units.qual.s;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.neu.common.RequiresPermissionsDesc;
import com.neu.common.validator.Order;
import com.neu.common.validator.Sort;
import com.neu.doc.AdminCategoryController_list;
import com.neu.doc.AdminAdController_ResOK;
import com.neu.doc.AdminAdController_list;
import com.neu.doc.AdminCategoryController_l1;
import com.neu.doc.ResponseOK;
import com.neu.domain.ShoppingAd;
import com.neu.domain.ShoppingCategory;
import com.neu.domain.ShoppingCategoryExample;
import com.neu.service.ShoppingCategoryService;
import com.neu.util.ResponseUtil;
import com.neu.vo.CategoryVo;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Validated
@RequestMapping("/admin/category")
@RestController
@CrossOrigin
public class AdminCategoryController {
    private final Log logger = LogFactory.getLog(AdminCategoryController.class);   
   
    
    @Autowired
    private ShoppingCategoryService shoppingCategoryService;
    
    
    //验证
    private Object validate(ShoppingCategory category) {
        String name = category.getName();
        if (StringUtils.isEmpty(name)) {
            return ResponseUtil.badArgument();
        }

        String level = category.getLevel();
        System.out.println(level);
        if (StringUtils.isEmpty(level)) {
            return ResponseUtil.badArgument();
        }
//        if (!level.equals("L1") && !level.equals("L2")) {
//            return ResponseUtil.badArgumentValue();
//        }

        Integer pid = category.getPid();
        if (level.equals("L2") && (pid == null)) {
            return ResponseUtil.badArgument();
        }

        return null;
    }
    
    
    @RequiresPermissions("admin:category:create")
    @RequiresPermissionsDesc(menu = {"商场管理", "类目管理"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody ShoppingCategory sy) {
    	
    	Object error = validate(sy);
    	System.out.println(error);
        if (error != null) {
            return error;
        }
        shoppingCategoryService.add(sy);
        return ResponseUtil.ok(sy);
    }
    	
    	
   
    
    
//    @ApiIgnore//使用该注解忽略这个API
//    @RequiresPermissions("admin:category:read")
//    @RequiresPermissionsDesc(menu = {"商场管理", "类目管理"}, button = "详情")
//  TODO
    



   @RequiresPermissions("admin:category:update")
   @RequiresPermissionsDesc(menu = {"商场管理", "类目管理"}, button = "编辑")
   @PostMapping("/update")
   public Object update(@RequestBody ShoppingCategory sy) {
	   Object error = validate(sy);
       if (error != null) {
           return error;
       }
       if(shoppingCategoryService.updateById(sy)==0) {
    	   return ResponseUtil.updatedDataFailed();
       }
       return ResponseUtil.ok(sy);
   }


    @RequiresPermissions("admin:category:delete")
    @RequiresPermissionsDesc(menu = {"商场管理", "类目管理"}, button = "删除")
    @PostMapping("/delete")
    public  Object delete(@RequestBody ShoppingCategory sy) {
    	 Integer id = sy.getId();
    	 System.out.println(id);
         if (id == null) {
             return ResponseUtil.badArgument();
         }
         shoppingCategoryService.deleteById(id);
         return ResponseUtil.ok();
     }



    @RequiresPermissions("admin:category:list")
    @ApiOperation(value="查询类目", notes="查询类目信息",response=AdminCategoryController_list.class)
    @GetMapping("/list")
    public Object list() {
        List<CategoryVo> categoryVoList = new ArrayList<>();

        List<ShoppingCategory> categoryList = shoppingCategoryService.queryByPid(0);
        for (ShoppingCategory category : categoryList) {
            CategoryVo categoryVO = new CategoryVo();
            categoryVO.setId(category.getId());
            categoryVO.setDesc(category.getDesc());
            categoryVO.setIconUrl(category.getIconUrl());
            categoryVO.setPicUrl(category.getPicUrl());
            categoryVO.setKeywords(category.getKeywords());
            categoryVO.setName(category.getName());
            categoryVO.setLevel(category.getLevel());

            List<CategoryVo> children = new ArrayList<>();
            List<ShoppingCategory> subCategoryList = shoppingCategoryService.queryByPid(category.getId());
            for (ShoppingCategory subCategory : subCategoryList) {
                CategoryVo subCategoryVo = new CategoryVo();
                subCategoryVo.setId(subCategory.getId());
                subCategoryVo.setDesc(subCategory.getDesc());
                subCategoryVo.setIconUrl(subCategory.getIconUrl());
                subCategoryVo.setPicUrl(subCategory.getPicUrl());
                subCategoryVo.setKeywords(subCategory.getKeywords());
                subCategoryVo.setName(subCategory.getName());
                subCategoryVo.setLevel(subCategory.getLevel());

                children.add(subCategoryVo);
            }

            categoryVO.setChildren(children);
            categoryVoList.add(categoryVO);
        }

        return ResponseUtil.okList(categoryVoList);
    }
    
    @ApiOperation(value="一级分类目录信息", notes="查询一级分类目录信息",response=AdminCategoryController_l1.class)
    @RequiresPermissions("admin:category:list")
    @GetMapping("/l1")
    public Object catL1() {
        // 所有一级分类目录
        List<ShoppingCategory> l1CatList = shoppingCategoryService.queryL1();
        List<Map<String, Object>> data = new ArrayList<>(l1CatList.size());
        for (ShoppingCategory category : l1CatList) {
            Map<String, Object> d = new HashMap<>(2);
            d.put("value", category.getId());
            d.put("label", category.getName());
            data.add(d);
        }
        return ResponseUtil.okList(data);
    }

}
